package com.v5ba;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.editor.Editor;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import com.intellij.util.xml.PsiClassConverter;
import org.jetbrains.annotations.NotNull;

public class SimpleCompletionContributor extends CompletionContributor {
    public SimpleCompletionContributor(){
        extend(CompletionType.BASIC,
                PlatformPatterns.psiElement(),
                new CompletionProvider<CompletionParameters>(){
                    @Override
                    protected void addCompletions(@NotNull CompletionParameters parameters,
                                                  @NotNull ProcessingContext context,
                                                  @NotNull CompletionResultSet result) {
                        PsiElement position = parameters.getPosition();
                        //JavaTokenType.ENUM_KEYWORD;
                        PsiElement originalPosition =  parameters.getOriginalPosition();

                        System.out.println("position: "+position.getText());
                        PsiElement element2 = PsiTreeUtil.getContextOfType(position, PsiElement.class);
                        System.out.println("position2:"+element2.getText());
                        if (element2.getNode().getElementType() == JavaTokenType.ENUM_KEYWORD){
                            System.out.println(11111222);
                        }
                        PsiClass psiClass = PsiTreeUtil.getContextOfType(element2, PsiClass.class);
                        System.out.println("psiClass isenum: "+psiClass.isEnum());

                        psiClass = PsiTreeUtil.getParentOfType(element2, PsiClass.class);
                        System.out.println("psiClass isenum2: "+psiClass.isEnum());



                        PrefixMatcher matcher = result.getPrefixMatcher();
                        String prefix = matcher.getPrefix();
                        // MARK: -- 当输入 "."时新增
                        if ("".equals(prefix)){
                            result.addElement(LookupElementBuilder.create("hello1"));
                        }
                        result.addElement(LookupElementBuilder.create("hello2"));
                    }
                });

    }
}
